package com.pansky.crm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pansky.crm.common.mybatisplus.SuperServiceImpl;
import com.pansky.crm.entity.Privilege;
import com.pansky.crm.entity.RolePrivilege;
import com.pansky.crm.entity.bo.RolePrivilegeDTO;
import com.pansky.crm.mapper.mysql.PrivilegeMapper;
import com.pansky.crm.mapper.mysql.RoleMapper;
import com.pansky.crm.mapper.mysql.RolePrivilegeMapper;
import com.pansky.crm.service.RolePrivilegeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/2/25 11:33
 * @description：RolePrivilege 服务
 */
@Service
@DS("mysqlDb")
@Slf4j
public class RolePrivilegeServiceImpl extends SuperServiceImpl<RolePrivilegeMapper, RolePrivilege> implements RolePrivilegeService {

    @Autowired
    private RolePrivilegeMapper rolePrivilegeMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Override
    public List<Privilege> listPrivilegeByRoleId(Long roleId) {
        return rolePrivilegeMapper.listPrivilegeByRoleId(roleId);
    }

    @Override
    public Boolean updateRolePrivilege(RolePrivilegeDTO updateDTO) {
        Integer roleId = updateDTO.getRoleId();
        // 传入的权限key
        List<String> privilegeKeyList = updateDTO.getPrivilegeKeyList();
        // 根据传入的权限key列表查询权限表
        QueryWrapper<Privilege> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(Privilege::getKey, privilegeKeyList);
        // 过滤排除不存在的权限key
        List<Privilege> privileges = privilegeMapper.selectList(queryWrapper);
        log.info("-----privileges is {}", privileges);
        // 2,3,4,7
        List<String> privilegeKeys = privileges.stream().map(Privilege::getKey).collect(Collectors.toList());
        log.info("-----privilegeKeys is {}", privilegeKeys);
        QueryWrapper<RolePrivilege> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RolePrivilege::getRoleId, roleId);
        // 查询已经存在的角色权限
        List<RolePrivilege> rolePrivileges = rolePrivilegeMapper.selectList(wrapper);
        log.info("-----rolePrivileges is {}", rolePrivileges);
        // 1,2,3,4,5,6
        List<String> rolePrivilegeKeys = rolePrivileges.stream().map(RolePrivilege::getPrivilegeKey).collect(Collectors.toList());
        // 需要增加的权限key
        List<String> deletePrivilegeKeys = rolePrivilegeKeys.stream().filter(key -> !privilegeKeys.contains(key)).collect(Collectors.toList());
        // 需要删除的权限key
        List<String> insertPrivilegeKeys = privilegeKeys.stream().filter(key -> !rolePrivilegeKeys.contains(key)).collect(Collectors.toList());
        log.info("-----insertPrivilegeKeys is {}", insertPrivilegeKeys);
        log.info("-----deletePrivilegeKeys is {}", deletePrivilegeKeys);
        // 如果权限key不为空，角色权限表删除掉不在权限key列表中的数据
        if (CollectionUtils.isNotEmpty(deletePrivilegeKeys)) {
            // delete:1,5,6  endi:2,3,4
            wrapper.lambda().in(RolePrivilege::getPrivilegeKey, deletePrivilegeKeys).eq(RolePrivilege::getRoleId, roleId);
            rolePrivilegeMapper.delete(wrapper);
        }
        // 新增不在权限key列表中的权限
        if (CollectionUtils.isNotEmpty(insertPrivilegeKeys)) {
            rolePrivilegeMapper.batchSaveRolePrivilegeByPrivilegeKeys(roleId, insertPrivilegeKeys);
        }
        return true;
    }
}
